import axios from "axios";
import ElementUI from 'element-ui'
// url 接口
// params  参数  get key=value  post 对象形式
// ifHideTit 是否隐藏提示有值表示隐藏 
async function get(url, params = "", ifHideTit) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: "",
      data: []
    }
    if (!url) {
      resData.errorMsg = "请输入接口!";
      ElementUI.Message.warning(resData.errorMsg);
      resolve(resData);
    }
    if (params) {
      params = `?${params}`
    }
    axios.get(url + params).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result || [];
      } else {
        resData.errorMsg = res.data.errorMsg || "服务器出小差了！";
        if (!ifHideTit) {
          ElementUI.Message.warning(resData.errorMsg);
        }
      }
      resolve(resData);
    }, err => {
      resData.errorMsg = "服务器出小差了！";
      if (!ifHideTit) {
        ElementUI.Message.error(resData.errorMsg)
      }
      resolve(resData);
    });
  });
}






async function post(url, params = {}, ifHideTit) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: "",
      data: []
    }
    if (!url) {
      resData.errorMsg = "请输入接口!";
      ElementUI.Message.warning(resData.errorMsg);
      resolve(resData);
    }
    axios.post(url, {
      ...params
    }).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result || [];
      } else {
        resData.errorMsg = res.data.errorMsg || "服务器出小差了！";
        if (!ifHideTit) {
          ElementUI.Message.warning(resData.errorMsg);
        }
      }
      resolve(resData);
    }, err => {
      resData.errorMsg = "服务器出小差了！";
      if (!ifHideTit) {
        ElementUI.Message.error(resData.errorMsg)
      }
      resolve(resData);
    });
  });
}


async function put(url, params = {}, ifHideTit) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: "",
      data: []
    }
    if (!url) {
      resData.errorMsg = "请输入接口!";
      ElementUI.Message.warning(resData.errorMsg);
      resolve(resData);
    }
    axios.put(url, {
      ...params
    }).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result || [];
      } else {
        resData.errorMsg = res.data.errorMsg || "服务器出小差了！";
        if (!ifHideTit) {
          ElementUI.Message.warning(resData.errorMsg);
        }
      }
      resolve(resData);
    }, err => {
      resData.errorMsg = "服务器出小差了！";
      if (!ifHideTit) {
        ElementUI.Message.error(resData.errorMsg)
      }
      resolve(resData);
    });
  });
}

async function del(url, params = {}, ifHideTit) {
  return new Promise((resolve) => {
    let resData = {
      success: false,
      errorMsg: "",
      data: []
    }
    if (!url) {
      resData.errorMsg = "请输入接口!";
      ElementUI.Message.warning(resData.errorMsg);
      resolve(resData);
    }
    axios.delete(url, {
      data: {
        ...params
      }
    }).then(res => {
      if (res.data.success) {
        resData.success = true;
        resData.data = res.data.result || [];
      } else {
        resData.errorMsg = res.data.errorMsg || "服务器出小差了！";
        if (!ifHideTit) {
          ElementUI.Message.warning(resData.errorMsg);
        }
      }
      resolve(resData);
    }, err => {
      resData.errorMsg = "服务器出小差了！";
      if (!ifHideTit) {
        ElementUI.Message.error(resData.errorMsg)
      }
      resolve(resData);
    });
  });
}

export default {
  get,
  post,
  put,
  del
}
